package com.tuanbiao.models.action;

import com.hzjq.data.hzjq.entity.CapRecordEntity;
import com.hzjq.lib.BlastKool;
import com.hzjq.lib.config.param.BlastLoader;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.data.callback.OnDetonatorsCallback;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/20 14:40
 */
public interface DetonatorAction {
    default void updateDetonator(CapRecordEntity entity, OnBaseResultCallback<CapRecordEntity> callback) {
        BlastKool.Companion.loader().db().updateCap(() -> {
            callback.onSuccess(entity);
            callback.onFinish();
        }, entity);
    }

    default void updateDetonator(List<CapRecordEntity> entities, OnBaseResultCallback<List<CapRecordEntity>> callback) {
        BlastKool.Companion.loader().db().updateCap(entities, () -> {
            callback.onSuccess(entities);
            callback.onFinish();
        });
    }

    default void deleteDetonator(CapRecordEntity entity, OnBaseResultCallback<CapRecordEntity> callback) {
        BlastKool.Companion.loader().db().deleteCap(() -> {
            callback.onSuccess(entity);
            callback.onFinish();
        }, entity);
    }

    default void deleteDetonator(List<CapRecordEntity> entities, OnBaseResultCallback<List<CapRecordEntity>> callback) {
        BlastKool.Companion.loader().db().deleteCap(entities, () -> {
            Timber.i("DetonatorAction#deleteDetonator");
            callback.onSuccess(entities);
            callback.onFinish();
        });
    }

    default void insertDetonator(String planId, CapRecordEntity insertEntity, CapRecordEntity selectEntity, OnBaseResultCallback<String> callback) {
        if (null == selectEntity) {
            BlastKool.Companion.loader().db().insertCap(planId, insertEntity, s -> {
                callback.onSuccess(s);
                callback.onFinish();
            });
        } else {
            BlastKool.Companion.loader().db().insertCap(planId, insertEntity, selectEntity, s -> {
                callback.onSuccess(s);
                callback.onFinish();
            });
        }
    }

    default void queryDetonatorByPlanId(String planId, OnDetonatorsCallback callback) {
        BlastKool.Companion.loader().db().queryCapsByPlan(planId, readResult -> {
            callback.onSuccess(readResult.getNewCaps(), readResult.getInfo());
            callback.onFinish();
        });
    }

    default void querySelectDetonator(OnDetonatorsCallback callback) {
        BlastKool.Companion.loader().db().querySelectCaps(entities -> {
            callback.onSuccess(entities, null);
            callback.onFinish();
        });
    }

    default void clearDetonator(String plantId, OnBaseResultCallback<Void> callback) {
        BlastKool.Companion.loader().db().clearPlanCaps(plantId, () -> {
            callback.onSuccess(null);
            callback.onFinish();
        });
    }


    default void overPlanCaps(String plantId, ArrayList<CapRecordEntity> caps,Runnable runnable){
        BlastKool.Companion.loader().quick().overPlanCap(plantId,caps,runnable);
    }

    /**
     * 雷管ID是否符合编码规则,214输入13位管壳码时判断
     * @param uid
     * @return
     */
    default boolean isCorrectCapByUidRule(String uid) {
       return BlastLoader.Companion.getDeviceBuyMillCallback()
                .isCorrectCapByUidRule(uid);
    }


    /**
     * 雷管ID是否符合编码规则,214输入13位UID码时判断
     * @param uid
     * @return
     */
    default boolean isCorrectDisplayUid(String uid) {
        return BlastKool.Companion.loader().isDisplayUid(uid);
    }
}
